var ImportWord = {
    data: {
		appId:-1,
		workId:1,
        displayResult: null
    },
    init: function (elem,appId,workId, callbackdisplayResult) {
        var self = this;
		self.data.appId=appId;
		self.data.workId=workId;
        self.data.displayResult = callbackdisplayResult;
        document.getElementById(elem).addEventListener("change", (event) => {
            self.readFileInputEventAsArrayBuffer(event, function (arrayBuffer) {
                var options = {
                    convertImage: mammoth.images.imgElement(function (image) {
                        return image.read("base64").then(function (imageBuffer) {

                            //上传图片
                            return {
                                //src: "data:" + image.contentType + ";base64," + imageBuffer
                                //src:"https://oss.uxhealthy.com/20210830/af550eb4-ac00-4f14-b904-3068d292b5a4.png"
                                src: self.uploadimage(image.altText, imageBuffer)
                            };
                        });
                    })
                };

                mammoth.convertToHtml({ arrayBuffer: arrayBuffer }, options)
                    .then(self.data.displayResult)
                    .done();
            });
        }, false);
    },
    readFileInputEventAsArrayBuffer: function (event, callback) {
        var file = event.target.files[0];

        var reader = new FileReader();

        reader.onload = function (loadEvent) {
            var arrayBuffer = loadEvent.target.result;
            callback(arrayBuffer);
        };

        reader.readAsArrayBuffer(file);
    },
    uploadimage: function (filename, imageBuffer) {
		var self=this;
        filename = filename || "default.png";
        filename = filename.indexOf('.') > -1 ? filename : (filename + ".png");
        var returl = "default.png";
        $.ajax({
            type: 'post',
            async: false,//必须改为同步
            url: '/basic/v1/OSS/UploadBase64?appId='+self.data.appId+'&workId='+self.data.workId+'&fileType=1&filename=' + filename,
            headers: {
                'Content-Type': 'text/plain; charset=utf-8'
            },
            data: imageBuffer,
            success: function (res) {
                //callback(res.data.url);
                //callback("https://oss.uxhealthy.com/20210830/af550eb4-ac00-4f14-b904-3068d292b5a4.png");
                //returl= "https://oss.uxhealthy.com/20210830/af550eb4-ac00-4f14-b904-3068d292b5a4.png";
                if (res.data.url) {
                    returl = res.data.url;
                }
            },
            error: function () {
                //callback("https://oss.uxhealthy.com/20210830/af550eb4-ac00-4f14-b904-3068d292b5a4.png");
                //return "";
            }
        });

        return returl;
    }
};

/*
(function () {

    ImportWord.init("document", (result) => {
        document.getElementById("output").value = result.value;
    });

})();
*/